package com.rpj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rpj.entity.Result;
import com.rpj.entity.UserDto;
import com.rpj.entity.UserEntity;
import com.rpj.secret.MD5Utils;
import com.rpj.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api("用户的增删改查")
@RestController
public class UserController {
@Autowired
private UserService userservice;

@PostMapping("/getToken")
@ApiOperation("获取数据 库秘钥")
public Object getSercret(HttpSession session,@ApiParam("登录用户名")@RequestParam("un")String un,@ApiParam("密码")@RequestParam("pwd")String pwd) {
	String str = MD5Utils.getMD5Str(un);
	UserEntity user = userservice.login(un, pwd);
	if(user!=null) {
		session.setAttribute("token", str);
		return str;
	}else {
		return "用户名不存在或密码错误";
	}
}

@ApiOperation("使用用户名和密码进行登录")
@RequestMapping(value = "/api/login",method = RequestMethod.POST)
public Object login(@ApiParam("用户名")@RequestParam("un")String un,
		@ApiParam("密码")@RequestParam("pwd")String pwd
	,@ApiParam("数据库秘钥")@RequestParam("token")String token) {
	System.out.println(un+pwd);
	
	String str = MD5Utils.getMD5Str(un);//获取用户名加密
	UserEntity login = userservice.login(un, pwd);
	System.out.println(login);
	if(str.equals(token)) {
		if(login!=null) {
			Result result = new Result(userservice.selByUserName(un),"",true);
			return result;
			
		}else {
			Result result = new Result("登录失败","",false);
			return result;
		}
		} else {
			Result result = new Result("数据库秘钥错误","",false);
			return result;
			}
			 
	
}
/**
 * 查询用户
 */
@ApiOperation("显示所有用户")
@RequestMapping(value="/api/GetUsersLayUI",method = RequestMethod.POST)
public Object findUser(UserDto Dto) {
	
	HashMap<String, Object> map = new HashMap<String, Object>();
	List<UserEntity> list = userservice.findUser(Dto);
	int i = userservice.countUser(Dto);
	map.put("code", 0);
	map.put("msg", "");
	map.put("data", list);
	map.put("count", i);
	return map;
	
}
/**
 * 添加用户
 */
@ApiOperation("添加用户")
@RequestMapping(value="/api/CreateUser",method =RequestMethod.POST)
public Object saveUser(HttpSession session,@ApiParam("姓名")@RequestParam("loginName")String loginName,@ApiParam("密码")@RequestParam("pwd")String pwd,@ApiParam("邮箱")@RequestParam("email")String email,@ApiParam("电话")@RequestParam("mtel")String mtel,@ApiParam("密钥")@RequestParam("token")String token) {
	int i=userservice.saveUser(loginName, pwd, email, mtel);
	String gettoken=(String) session.getAttribute("token");
	//String str = MD5Utils.getMD5Str(loginName);//获取用户名加密
	 //String uuid = UUID.randomUUID().toString().replace("-", "");
	 UserEntity entity = new UserEntity();
	 if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","添加成功",true);
		 }else{
				return new Result("","添加失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
		
		
}
/**
 * 修改用户
 */
@ApiOperation("修改用户")
@RequestMapping(value="/api/UpdateUser",method = RequestMethod.POST)
public Object updateUser(HttpSession session,@ApiParam("邮箱")@RequestParam("email")String email,@ApiParam("电话")@RequestParam("mtel")String mtel,@ApiParam("名字")@RequestParam("loginName")String loginName,@ApiParam("密钥")@RequestParam("token")String token) {
	String gettoken=(String) session.getAttribute("token");
	UserEntity userEntity = new UserEntity();
	userEntity.setEmail(email);
	userEntity.setPhone(mtel);
	userEntity.setName(loginName);
	int i = userservice.updateUser(userEntity);
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","修改成功",true);
		 }else{
				return new Result("","修改失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
		
}
/**
 * 删除用户
 */
@ApiOperation("删除用户")
@RequestMapping(value="/api/DeleteUser",method = RequestMethod.POST)
public Object delUser(HttpSession session,@ApiParam("id")@RequestParam("uid")String uid,@ApiParam("密钥")@RequestParam("token")String token) {
	UserEntity userEntity = new UserEntity();
	userEntity.setId(uid);
	int i = userservice.delUser(userEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","删除成功",true);
		 }else{
				return new Result("","删除失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
@ApiOperation("重置密码")
@RequestMapping(value="/api/ResetPassword",method = RequestMethod.POST)
public Object resetUser(@ApiParam("姓名")@RequestParam("un")String un,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	UserEntity userEntity = new UserEntity();
	userEntity.setName(un);
	int i = userservice.resetUser(userEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","重置成功",true);
		 }else{
				return new Result("","重置失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
@ApiOperation("状态锁定")
@RequestMapping(value="/api/LockUser",method = RequestMethod.POST)
public Object lockUser(@ApiParam("姓名")@RequestParam("un")String un,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	UserEntity userEntity = new UserEntity();
	userEntity.setName(un);
	int i = userservice.lockUser(userEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","锁定成功",true);
		 }else{
				return new Result("","锁定失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
@ApiOperation("状态解锁")
@RequestMapping(value="/api/UnLockUser",method = RequestMethod.POST)
public Object nolockUser(@ApiParam("姓名")@RequestParam("un")String un,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	UserEntity userEntity = new UserEntity();
	userEntity.setName(un);
	int i = userservice.nolockUser(userEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","解锁成功",true);
		 }else{
				return new Result("","解锁失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
}